Namespace DataStream
    Friend Class ConsultToStreamConverter

        Friend Shared Function Convert(ByVal Consult As DataLogging.ConsultLog) As DataStream.StreamLog
            Dim streamLog As New DataStream.StreamLog()
            Dim stream As DataStream.Stream
            streamLog.StartTime = Consult.Item(0).TimeStamp

            ' Specify just to brute force continue with the next.. ignore all exceptions here!
            On Error Resume Next

            For Each entry As Consult_Args In Consult
                stream = New Stream()

                stream.Add(New DataStream.StreamItem("RPM", StreamItemType.RPM, entry.rpm))
                stream.Add(New StreamItem("AAC", StreamItemType.Percent, entry.Aac))
                stream.Add(New StreamItem("AFM", StreamItemType.Percent, entry.AfmLeft))
                stream.Add(New StreamItem("BVolts", StreamItemType.Volts, entry.bVoltage))
                stream.Add(New StreamItem("Aircon Sw", StreamItemType.Switch, entry.ACon))
                stream.Add(New StreamItem("Pw Steering Sw", StreamItemType.Switch, entry.PowerStsw))
                stream.Add(New StreamItem("Park Neautral Sw", StreamItemType.Switch, entry.ParkNsw))
                stream.Add(New StreamItem("Start Sw", StreamItemType.Switch, entry.StartSig))
                stream.Add(New StreamItem("Throttle Sw", StreamItemType.Switch, entry.ThrottleSw))
                stream.Add(New StreamItem("Aircon Relay", StreamItemType.Switch, entry.ACrelay))
                stream.Add(New StreamItem("Fuel Pump Relay", StreamItemType.Switch, entry.FuelRelay))
                stream.Add(New StreamItem("VCT Solenoid", StreamItemType.Switch, entry.VCT))
                stream.Add(New StreamItem("Fan Hi Relay", StreamItemType.Switch, entry.FanHi))
                stream.Add(New StreamItem("Fan Lo Relay", StreamItemType.Switch, entry.FanLo))
                stream.Add(New StreamItem("PReg Control Valve", StreamItemType.Switch, entry.PRegControlValue))
                stream.Add(New StreamItem("Wastegate Solenoid", StreamItemType.Switch, entry.WastegateSoleniod))
                stream.Add(New StreamItem("IACV Solenoid", StreamItemType.Switch, entry.IACVsoleniod))
                stream.Add(New StreamItem("EGR Solenoid", StreamItemType.Switch, entry.EGRsoleniod))
                stream.Add(New StreamItem("Ignition Timing", StreamItemType.Ignition, entry.Ignition))
                stream.Add(New StreamItem("O2 Sensor (narrowband)", StreamItemType.mVolts, entry.o2Left))
                stream.Add(New StreamItem("Load", StreamItemType.Load, entry.TP))
                stream.Add(New StreamItem("Speed", StreamItemType.Speed, entry.Speed))
                stream.Add(New StreamItem("Temp", StreamItemType.TEMP, entry.Temp))
                stream.Add(New StreamItem("TPS", StreamItemType.mVolts, entry.Tps))
                stream.Add(New StreamItem("VE", StreamItemType.Volts, entry.VeTrace))

                stream.Add(New StreamItem("ADC1", StreamItemType.mVolts, entry.BKAD(0)))
                stream.Add(New StreamItem("ADC2", StreamItemType.mVolts, entry.BKAD(1)))
                stream.Add(New StreamItem("ADC3", StreamItemType.mVolts, entry.BKAD(3)))

                stream.Add(New StreamItem("SW1", StreamItemType.Switch, entry.BKI(0)))
                stream.Add(New StreamItem("SW2", StreamItemType.Switch, entry.BKI(1)))
                stream.Add(New StreamItem("SW3", StreamItemType.Switch, entry.BKI(2)))
                stream.Add(New StreamItem("SW4", StreamItemType.Switch, entry.BKI(3)))
                stream.Add(New StreamItem("SW5", StreamItemType.Switch, entry.BKI(4)))

                For i As Integer = 0 To 3
                    If Not IsNothing(entry.BKO(i)) Then
                        stream.Add(New StreamItem("Aux1", StreamItemType.Percent, entry.BKO(i)))
                        stream.Add(New StreamItem("Aux" & i & "State", StreamItemType.Switch, entry.BKO_OnOff(i)))
                    End If
                Next

                For i As Integer = 0 To 19
                    If Not IsNothing(entry.Trigger(i)) Then
                        stream.Add(New StreamItem("TR" & i, StreamItemType.Switch, entry.Trigger(i)))
                    End If
                Next

                For i As Integer = 0 To 2
                    If Not IsNothing(entry.ADClookup(i)) Then
                        stream.Add(New StreamItem("ADC Lookup " & i, StreamItemType.ADLookup, entry.ADClookup(i)))
                    End If
                Next

                stream.Add(New StreamItem("Flash Map", StreamItemType.AFR, entry.FlashMap))
                stream.Add(New DataStream.StreamItem("CPU Usage", StreamItemType.Percent, entry.CPUTime))

                streamLog.Add(stream)
            Next

            Return streamLog
        End Function

    End Class

End Namespace


